//
//  DetailVerifyView.m
//  shusheng
//
//  Created by rimi on 16/7/20.
//  Copyright © 2016年 yuying. All rights reserved.
//

#import "DetailVerifyView.h"

@interface DetailVerifyView()<UITextFieldDelegate>
{
    int second;
}
@property(strong,nonatomic)UILabel      *timeLabel;
@property(strong,nonatomic)NSTimer      *timer;
@property(strong,nonatomic)UIView       *showView;
@property(strong,nonatomic)UILabel      *titleLabel;
@property(strong,nonatomic)UITextField  *phoneTextField;
@property(strong,nonatomic)UILabel      *phoneLabel;
@property(strong,nonatomic)UIView       *phoneView;
@property(strong,nonatomic)UITextField  *verifyTextField;
@property(strong,nonatomic)UILabel      *verifuLabel;
@property(strong,nonatomic)UIView       *verifyView;
@property(strong,nonatomic)UIButton     *verifyButton;
@property(strong,nonatomic)UIButton     *affirmButton;
@property(strong,nonatomic)UIButton     *backButton;
- (void)initializeInterface;  //初始化用户界面
-(void)phoneClick;
-(void)phoneCancle;
-(void)verifyClick;
-(void)verifyCancle;
@end

@implementation DetailVerifyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGB_COLOR(47, 47, 47, 0.5);
        self.phoneTextField.delegate = self;
        self.verifyTextField.delegate = self;
        [self initializeInterface];
    }
    return self;
}


-(void)initializeInterface{
    [self addSubview:self.showView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.phoneTextField];
    [self addSubview:self.phoneLabel];
    [self addSubview:self.phoneView];
    [self addSubview:self.verifyTextField];
    [self addSubview:self.verifuLabel];
    [self addSubview:self.verifyView];
    [self addSubview:self.verifyButton];
    [self addSubview:self.affirmButton];
    [self addSubview:self.backButton];
    
}
#pragma mark -- Action
//animation
-(void)phoneClick{
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.phoneLabel.frame = CGRectMake(CGRectGetMinX(self.phoneTextField.frame)+7, CGRectGetMinY(self.phoneTextField.frame)-15, 200, 30);
        self.phoneView.backgroundColor = [UIColor blueColor];
        [self.phoneLabel setTextColor:[UIColor blueColor]];
        self.phoneLabel.font = [UIFont systemFontOfSize:12];
    } completion:nil];
  
}
-(void)phoneCancle{
    if ([self.phoneTextField.text isEqualToString:@""]) {
        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            self.phoneLabel.frame = CGRectMake(CGRectGetMinX(self.phoneTextField.frame)+5, CGRectGetMinY(self.phoneTextField.frame)+10, 200, 30);
            self.phoneView.backgroundColor = [UIColor blackColor];
            [self.phoneLabel setTextColor:[UIColor blackColor]];
            self.phoneLabel.font = [UIFont systemFontOfSize:17];
        } completion:nil];
    }
}
-(void)verifyClick{
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.verifuLabel.frame = CGRectMake(CGRectGetMinX(self.verifyTextField.frame)+7, CGRectGetMinY(self.verifyTextField.frame)-15, 200, 30);
        self.verifyView.backgroundColor = [UIColor blueColor];
        [self.verifuLabel setTextColor:[UIColor blueColor]];
        self.verifuLabel.font = [UIFont systemFontOfSize:12];
    } completion:nil];
}
-(void)verifyCancle{
    [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.verifuLabel.frame = CGRectMake(CGRectGetMinX(self.verifyTextField.frame)+5, CGRectGetMinY(self.verifyTextField.frame)+10, 200, 30);
        self.verifyView.backgroundColor = [UIColor blackColor];
        [self.verifuLabel setTextColor:[UIColor blackColor]];
        self.verifuLabel.font = [UIFont systemFontOfSize:17];
    } completion:nil];
}
//buttonAction
-(void)verifyBtnAction{
    AVUser *user = [AVUser currentUser];
    [user setObject:self.phoneTextField.text forKey:@"mobilePhoneNumber"];
    
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [MBProgressHUD showSuccess:@"验证码已发送"];
            [self addSubview:self.timeLabel];
            second = 60;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeCountDown) userInfo:nil repeats:YES];
            _timeLabel.text = [NSString stringWithFormat:@"%d",second];
            [_affirmButton setUserInteractionEnabled:YES];
            _affirmButton.titleLabel.alpha = 1;
            [_verifyButton setUserInteractionEnabled:NO];
            _verifyButton.titleLabel.alpha = 0.3;
        }else{
            [MBProgressHUD showError:error.detialMessage];
        }
    }];
//    [self verifyAction];
}
-(void)verifyAction{
    [AVUser verifyMobilePhone:self.verifyTextField.text withBlock:^(BOOL succeeded, NSError *error) {
        if(succeeded){
            [MBProgressHUD showSuccess:@"验证成功"];
            self.mine.phoneVerifyView.yorNVerifyLabel.text = @"已验证";
            [self.mine.phoneVerifyView.yorNVerifyLabel setTextColor:[UIColor blueColor]];
            [self.mine.phoneVerifyView.yorNVerifyImg setImage:[UIImage imageNamed:@"已验证"]];
           
            [self backBtnClick];
        }else{
            [MBProgressHUD showError:error.detialMessage];
        }
    }];
}
-(void)backBtnClick{
//    self.phoneTextField.text = @"";
    self.verifyTextField.text = @"";
    [_verifyButton setUserInteractionEnabled:YES];
    _verifyButton.titleLabel.alpha = 1;
    [_affirmButton setUserInteractionEnabled:NO];
    _affirmButton.titleLabel.alpha = 0.3;
    [_timer invalidate];
    [_timeLabel removeFromSuperview];
    [self removeFromSuperview];

}
-(void)timeCountDown{
    second --;
    //修改倒计时标签显示内容
    _timeLabel.text = [NSString stringWithFormat:@"%d",second];
    if (second == 0) {
        [_timer invalidate];
        [_timeLabel removeFromSuperview];
        [_verifyButton setUserInteractionEnabled:YES];
        _verifyButton.titleLabel.alpha = 1;
    }
}

#pragma mark -- UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.phoneTextField) {
        [self phoneCancle];
    }else if (textField == self.verifyTextField){
        [self verifyCancle];
    }
 
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.phoneTextField) {
        [self phoneClick];
    }else if (textField == self.verifyTextField){
        [self verifyClick];
    }
   
}


#pragma mark -- Getter
-(UIView *)showView{
    if (!_showView) {
        _showView = [[UIView alloc]init];
        _showView.frame = CGRectMake(30, 200, 350, 250);
        _showView.backgroundColor = [UIColor whiteColor];
    }
    return _showView;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.bounds = CGRectMake(0, 0, 100, 30);
        _titleLabel.center = CGPointMake(CGRectGetMinX(self.showView.frame)+60, CGRectGetMinY(self.showView.frame)+20);
        [_titleLabel setTextColor:[UIColor blackColor]];
        _titleLabel.font = [UIFont systemFontOfSize:20];
        _titleLabel.text = @"手机验证";
    }
    return _titleLabel;
}

-(UITextField *)phoneTextField{
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc]init];
        _phoneTextField.backgroundColor = [UIColor clearColor];
        _phoneTextField.bounds = CGRectMake(0, 0, CGRectGetWidth(self.showView.bounds)-20, 50);
        _phoneTextField.center = CGPointMake(CGRectGetMidX(self.showView.frame), CGRectGetMaxY(self.titleLabel.frame)+50);
        
    }
    return _phoneTextField;
}
-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.bounds = CGRectMake(0, 0, CGRectGetWidth(self.phoneTextField.bounds)/3, 30);
        _timeLabel.center = CGPointMake(CGRectGetMaxX(self.phoneTextField.frame)-45, CGRectGetMidY(self.phoneTextField.frame)-5);
        _timeLabel.font = [UIFont systemFontOfSize:20];
        [_timeLabel setTextColor:[UIColor blackColor]];
    }
    return _timeLabel;
}
-(UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc]init];
        _phoneLabel.frame = CGRectMake(CGRectGetMinX(self.phoneTextField.frame)+5, CGRectGetMinY(self.phoneTextField.frame)+10, 200, 30);
        _phoneLabel.text = @"请输入手机号码";
    }
    return _phoneLabel;
}
-(UIView *)phoneView{
    if (!_phoneView) {
        _phoneView = [[UIView alloc]init];
        _phoneView.frame = CGRectMake(CGRectGetMinX(self.phoneTextField.frame), CGRectGetMaxY(self.phoneTextField.frame), CGRectGetWidth(self.phoneTextField.bounds)*2/3, 1);
        _phoneView.backgroundColor = [UIColor blackColor];
    }
    return _phoneView;
}
-(UITextField *)verifyTextField{
    if (!_verifyTextField) {
        _verifyTextField = [[UITextField alloc]init];
        _verifyTextField.backgroundColor = [UIColor clearColor];
        _verifyTextField.frame = CGRectMake(CGRectGetMinX(self.phoneTextField.frame), CGRectGetMaxY(self.phoneTextField.frame)+25, CGRectGetWidth(self.phoneTextField.bounds)*2/3-5, 50);
    }
    return _verifyTextField;
}
-(UILabel *)verifuLabel{
    if (!_verifuLabel) {
        _verifuLabel = [[UILabel alloc]init];
        _verifuLabel.frame = CGRectMake(CGRectGetMinX(self.verifyTextField.frame)+5, CGRectGetMinY(self.verifyTextField.frame)+10, 200, 30);
        _verifuLabel.text = @"请输入短信验证码";
    }
    return _verifuLabel;
}
-(UIView *)verifyView{
    if (!_verifyView) {
        _verifyView = [[UIView alloc]init];
        _verifyView.frame = CGRectMake(CGRectGetMinX(self.verifyTextField.frame), CGRectGetMaxY(self.verifyTextField.frame), CGRectGetWidth(self.verifyTextField.bounds), 1);
        _verifyView.backgroundColor = [UIColor blackColor];
    }
    return _verifyView;
}
-(UIButton *)verifyButton{
    if (!_verifyButton) {
        _verifyButton = [[UIButton alloc]init];
        _verifyButton.bounds = CGRectMake(0, 0, CGRectGetWidth(self.phoneTextField.bounds)*1/3-5, 30);
        _verifyButton.center = CGPointMake(CGRectGetMaxX(self.phoneTextField.frame)-52.5, CGRectGetMidY(self.verifyTextField.frame));
        _verifyButton.backgroundColor = [UIColor clearColor];
        [_verifyButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        _verifyButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_verifyButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_verifyButton addTarget:self action:@selector(verifyBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _verifyButton;
}

-(UIButton *)affirmButton{
    if (!_affirmButton) {
        _affirmButton = [[UIButton alloc]init];
        _affirmButton.bounds = CGRectMake(0, 0, CGRectGetWidth(self.phoneTextField.bounds)*1/3-5, 30);
        _affirmButton.center = CGPointMake(CGRectGetMaxX(self.phoneTextField.frame)-52.5, CGRectGetMaxY(self.verifyView.frame)+30);
        _affirmButton.backgroundColor = [UIColor clearColor];
        [_affirmButton setUserInteractionEnabled:NO];
        _affirmButton.titleLabel.alpha = 0.3;
        [_affirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_affirmButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_affirmButton addTarget:self action:@selector(verifyAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _affirmButton;
}

-(UIButton *)backButton{
    if (!_backButton) {
        _backButton = [[UIButton alloc]init];
        _backButton.frame = CGRectMake(CGRectGetMinX(self.verifyView.frame), CGRectGetMaxY(self.verifyView.frame)+15, CGRectGetWidth(self.phoneTextField.bounds)*1/3-5, 30);
        _backButton.backgroundColor = [UIColor clearColor];
        [_backButton setTitle:@"取消" forState:UIControlStateNormal];
        [_backButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}
@end
